package com.mbigger.weather.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.mbigger.weather.vo.City;

@Component
public class WeatherDataSyncJob {
    private static final Log logger = LogFactory.getLog(WeatherDataSyncJob.class);
    @Autowired
    private CityDataService cityDataService;
    @Autowired
    private WeatherDataService weatherDataService;
    
    @Scheduled(fixedDelay=20000)
    public void execute() {
        logger.info("Weather Data Sync Job. Start!");
        // 获取城市列表
        List<City> cityList = null;
        try {
            cityList = cityDataService.listCity();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        // 遍历城市ID获取天气
        for (City city : cityList) {
            String cityId = city.getCityId();
            logger.info("Weather Data Sync City Id: " + cityId);
            weatherDataService.getDataByCityId(cityId);
        }
        logger.info("Weather Data Sync Job. Finish!");
    }

}
